﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Perform
{
    public partial class frmResaleAdd : Form
    {
        
        public frmResaleAdd()
        {
            InitializeComponent();
        }

        private void frmResaleAdd_Load(object sender, EventArgs e)
        {
            string sql = "select fid from flower";
            SqlDataReader sdr = Dal.DataHelper.ExecuteReader(sql);
            while (sdr.Read()) { cmbfid.Items.Add(sdr["fid"].ToString()); }
        }

        private void cmbfid_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "select * from flower where fid='" + cmbfid.Text + "'";
            SqlDataReader sdr = Dal.DataHelper.ExecuteReader(sql);
            while (sdr.Read())
            {
                tbfname.Text = sdr["fname"].ToString();
                tbfsalecost.Text = sdr["fsalecost"].ToString();
            }
        }

        private void dtptime_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string id = cmbfid.Text.Trim();
            string number = tbinumber.Text.Trim();
            string time = dtptime.Text.Trim();
            if (id == "--请选择编号--" || number == "")
            {
                MessageBox.Show("请完善信息！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            try
            {
                string sql = "select count(1) from resale where rid=''";
                if (Dal.DataHelper.sqlExecuteScalar(sql) == 0)
                {
                    sql = string.Format("insert into resale values('{0}','{1}','{2}')", id, number, time);
                    if (Dal.DataHelper.sqlExecuteNonQuery(sql) > 0)
                    {
                        MessageBox.Show("添加零售信息成功！", "信息提示");
                    }
                }
                else
                {
                    MessageBox.Show("添加零售信息失败！", "信息提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbfname.Text = "";
            tbfsalecost.Text = "";
            tbinumber.Text = "";
            cmbfid.Text = "--请选择编号--";
            dtptime.Text = DateTime.Now.ToLongDateString();
        }
    }
}
